﻿using Asp.Versioning;
using FlyingEye.DeviceMaintenance;
using FlyingEye.DeviceMaintenance.Repositories;
using FlyingEye.DeviceMaintenanceServices;
using FlyingEye.Exceptions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    /// <summary>
    /// 设备部件特征点
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/device-ops-service/v{version:apiVersion}/device-part-characteristics")]
    [ControllerName("DevicePartCharacteristic")]
    [Authorize]
    public class DevicePartCharacteristicController : DefaultController
    {
        private readonly IDevicePartCharacteristicRepository _devicePartCharacteristicRepository;

        public DevicePartCharacteristicController(IDevicePartCharacteristicRepository devicePartCharacteristicRepository)
        {
            _devicePartCharacteristicRepository = devicePartCharacteristicRepository;
        }

        [HttpGet("{id}", Name = "GetByDevicePartCharacteristicId")]
        public async Task<ActionResult<DevicePartCharacteristicResponse>> GetAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var model = await _devicePartCharacteristicRepository.FindAsync(id);

                if (model == null)
                {
                    throw new ResourceNotFoundException("特征点不存在！");
                }

                return ObjectMapper.Map<DevicePartCharacteristicModel, DevicePartCharacteristicResponse>(model);
            });
        }

        [HttpGet()]
        public async Task<ActionResult<List<DevicePartCharacteristicResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                var list = await _devicePartCharacteristicRepository.GetListAsync();

                return ObjectMapper.Map<List<DevicePartCharacteristicModel>, List<DevicePartCharacteristicResponse>>(list);
            });
        }

        [HttpPost]
        public async Task<ActionResult<DevicePartCharacteristicResponse>> InsertAsync([FromBody] CreateDevicePartCharacteristicRequest request)
        {
            return await this.InsertInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<CreateDevicePartCharacteristicRequest, DevicePartCharacteristicModel>(request);

                model = await _devicePartCharacteristicRepository.InsertAsync(model);

                return ObjectMapper.Map<DevicePartCharacteristicModel, DevicePartCharacteristicResponse>(model);
            }, "GetByDevicePartCharacteristicId", e => new { e.Id });
        }

        [HttpPut]
        public async Task<ActionResult<DevicePartCharacteristicResponse>> UpdateAsync([FromBody] UpdateDevicePartCharacteristicRequest request)
        {
            return await this.PutInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<UpdateDevicePartCharacteristicRequest, DevicePartCharacteristicModel>(request);

                model = await _devicePartCharacteristicRepository.UpdateAsync(model);

                return ObjectMapper.Map<DevicePartCharacteristicModel, DevicePartCharacteristicResponse>(model);
            });
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteAsync(Guid id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                var model = await _devicePartCharacteristicRepository.FindAsync(id);

                if (model == null)
                {
                    throw new ResourceNotFoundException("特征点不存在！");
                }

                await _devicePartCharacteristicRepository.DeleteAsync(model);
            });
        }
    }
}
